package 回溯算法.子集_组合_排列;

import java.util.LinkedList;
import java.util.List;

public class 全排列_46 {

    public static void main(String[] args) {
        全排列_46.permute(new int[]{1, 2, 3});
    }

    static List<List<Integer>> res = new LinkedList<>();

    public static List<List<Integer>> permute(int[] nums) {
        LinkedList<Integer> track = new LinkedList<>();

        backtrack(nums, track);
        return res;
    }

    public static void backtrack(int[] nums, LinkedList<Integer> track) {

        if (track.size() == nums.length) {//到达了叶子节点
            res.add(new LinkedList(track));
            return;
        }

        for (int i = 0; i < nums.length; i++) {
            // 排除在 track 中已经选择过的数字
            if (track.contains(nums[i])) {
                continue;
            }
            track.add(nums[i]);
            backtrack(nums, track);
            track.removeLast();
        }

    }
}
